package com.shop.controller;

import com.alibaba.fastjson.JSONObject;
import com.shop.pojo.User;
import com.shop.result.JsonResult;
import com.shop.result.Result;
import com.shop.service.LoginService;
import com.shop.util.BaseUtil;
import com.shop.vo.LoginVo;
import net.sf.json.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.UnsupportedEncodingException;
import java.util.Map;

@Controller
@RequestMapping("/login")
public class LoginController {
    @Autowired
    private LoginService loginService;

    //登录校验接口
    @RequestMapping("/do_login")
    @ResponseBody
    public JsonResult doLogin(HttpServletRequest request,HttpServletResponse response) {
        String token = loginService.login(request,response);
        return new JsonResult(true,token);
    }

    //注册接口
    @RequestMapping("/do_regsiter")
    @ResponseBody
    public JsonResult doRegsiter(HttpServletRequest request,HttpServletResponse response) throws Exception {
//        String data = request.getParameter("data");
//        data = BaseUtil.dataTranscoding(data);
//
//        String json = BaseUtil.toJson(data);
//        User user = JSONObject.parseObject(json, User.class);

        String userName = request.getParameter("userName");    //账号，默认是手机号码
        String password = request.getParameter("password").replace(" ","");    //密码
        String phone = request.getParameter("phone").replace(" ","");

        JSONObject userJson = new JSONObject();
        userJson.put("userName",userName);
        userJson.put("password",password);
        userJson.put("phone",phone);
        User user = JSONObject.parseObject(BaseUtil.toJson(userJson), User.class);

        JsonResult returnMsg = loginService.regsiter(user);
        return returnMsg;
    }


}
